// pages/record/received.js
const app = getApp()
Page({
	data: {
		// tab切换
		currentTab: 0,
		userInfo: {},
		my_reward_person: 0,
		my_reward_money: {
			rmb: 0,
			mag: 0
		},
		get_page: 1,
		set_page: 1,
		get_data: {}, //收到的数据列表
		set_data: {},
		height1: 0,
		height2: 0,
		is_wxpay:0,
		setting:{}
	},
	onLoad: function () {
		var that = this;
		//是否开启微信支付
		app.connect('index/setting', {}, function (res) {
			that.setData({
				is_wxpay: res.is_wxpay,
				setting:res
			})
		});
		app.login(function (userInfo) {
			that.setData({
				userInfo: userInfo.data
			});

			//收到的赞赏次数
			app.connect('my/my_reward_count', {
				uid: that.data.userInfo.uid
			}, function (e) {
				that.setData({
					my_reward_person: e
				})
			});
			//收入
			app.connect('my/my_reward_money', {
				uid: that.data.userInfo.uid
			}, function (e) {
				that.setData({
					my_reward_money: e
				})
			}, true);

			//我收到的赞赏列表
			app.connect('account/my_reward_get', {
				openid: that.data.userInfo.openId,
				uid: that.data.userInfo.uid
			}, function (res) {
				that.setData({
					get_data: res.data
				});
				// that.jiedian();
			})

			//我发出的赞赏
			app.connect('account/my_reward_set', {
				openid: that.data.userInfo.openId,
				uid: that.data.userInfo.uid
			}, function (res) {
				console.log(res);
				that.setData({
					set_data: res.data
				});
				that.jiedian();
                  
			})
		});

	},
	//获得推荐和最新部分的节点高度
	jiedian: function () {
		let that = this;

		wx.getSystemInfo({
			success: function (re) {
			
				var query = wx.createSelectorQuery()
				query.select('#get').boundingClientRect()
				query.selectViewport().scrollOffset()
				query.exec(function (res) {
					that.setData({
						height1: res[0].height < re.windowHeight ? re.windowHeight - 70  : res[0].height
					});
                         
				})

				var query = wx.createSelectorQuery()
				query.select('#set').boundingClientRect()
				query.selectViewport().scrollOffset()
				query.exec(function (res) {

					that.setData({
						height2: res[0].height < re.windowHeight ? re.windowHeight - 70 : res[0].height
					});
                         console.log(that.data.height1 + ' <> ' + that.data.height2);
				})
               
			}
		})




	},
	/**
	 * 滑动切换tab
	 */
	bindChange: function (e) {

		var that = this;
		that.setData({
			currentTab: e.detail.current
		});

	},
	/**
	 * 点击tab切换
	 */
	swichNav: function (e) {
		var that = this;

		if (this.data.currentTab === e.target.dataset.current) {
			return false;
		} else {
			that.setData({
				currentTab: e.target.dataset.current
			})
		}
	},
	//触底切页
	onReachBottom: function () {
		console.log('触底');
		let that = this;
		//推荐数据页面
		var get_page = that.data.get_page;
		//最新数据页码
		var set_page = that.data.set_page;
		var currentTab = that.data.currentTab;
		wx.showLoading()
		//推荐数据
		if (currentTab == 0) {
			app.connect('account/my_reward_get', {
				openid: that.data.userInfo.openId,
				uid: that.data.userInfo.uid,
				page: get_page + 1
			}, function (res) {
				if (res.data.length == 0) {
					wx.showToast({
						title: '到底啦',
						icon: 'none'
					})
					return false;
				}
				var get_data = that.data.get_data;
				that.setData({
					get_data: get_data.concat(res.data)
				});
				that.jiedian();
				wx.hideLoading()
			})

			that.setData({
				get_page: get_page + 1
			});

		} else if (currentTab == 1) {
			app.connect('account/my_reward_set', {
				openid: that.data.userInfo.openId,
				uid: that.data.userInfo.uid,
				page: set_page + 1
			}, function (res) {
				if (res.data.length == 0) {
					wx.showToast({
						title: '到底啦',
						icon: 'none'
					})
					return false;
				}
				var set_data = that.data.set_data;
				that.setData({
					set_data: set_data.concat(res.data)
				});
				that.jiedian();
				wx.hideLoading()
			})

			that.setData({
				set_page: set_page + 1
			});

		}
	},
	look:function(e){
		console.log(e);
		var content_id = e.currentTarget.dataset.content_id;
		var is_del = e.currentTarget.dataset.is_del;
		if(is_del == 1) {
			wx.showToast({
				title: '照片已删除，无法查看',
				icon:'none'
			})
		}else{
			wx.navigateTo({
				url: '../look/index?content_id=' + content_id,
			})
		}
	},
	//转发
	onShareAppMessage: function (res) {
		let that = this;
		if (res.from == 'menu') {
			return {
				title: '麦颜值',
				path: 'pages/index/index',
			}
		}
		var content_id = res.target.dataset.content_id;
		var imgUrl = res.target.dataset.imgurl;
		var nick = res.target.dataset.nick;
		console.log(res);
		return {
			title: '@' + nick + ' 在麦颜值发表的作品',
			path: 'pages/look/index?content_id=' + content_id + '&type=1',
			imageUrl: imgUrl
		}
	},
	lookImg: function (e) {

		var content_id = e.currentTarget.dataset.content_id;
		var img_id = e.currentTarget.dataset.img_id;
		wx.navigateTo({
			url: '../look/index?content_id=' + content_id + "&img_id=" + img_id,
		})
	}
})